package ch.zhaw.cctd.domain.map;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.io.Serializable;

/**
 * Repräsentiert eine quadratische Region (quadratisches Feld). Speichert die
 * Kooridinaten und die grösse in einem Rectangle.
 * 
 * @author Rolf Koch
 * 
 */
public class Region extends Rectangle implements Serializable {

	/**
	 * Serializable ID
	 */
	private static final long serialVersionUID = -4200967409487789290L;

	/**
	 * Default Constructor
	 * 
	 * @param point
	 * @param dimension
	 */
	public Region(Point point, Dimension d) {
		super(point, d);
	}

	/**
	 * Get a Random Point within the Region
	 * 
	 * @return
	 */
	public Point getRandomPoint() {
		int x = (int) (super.getX() + Math.floor(Math.random() * (double) super.getWidth()));
		int y = (int) (super.getY() + Math.floor(Math.random() * (double) super.getHeight()));
		return new Point(x, y);
	}

	/**
	 * Center Point of the Region (Rounded to the left top)
	 * 
	 * @return
	 */
	public Point getCenter() {
		return new Point((int) super.getCenterX(), (int) super.getCenterY());
	}

	/**
	 * Set the Region to a new CenterPoint
	 * 
	 * @param p
	 */
	public void setNewCenter(Point centerPoint) {
		int x = (int) (centerPoint.getX() - super.getWidth() / 2);
		int y = (int) (centerPoint.getY() - super.getHeight() / 2);
		this.setLocation(new Point(x, y));
	}

}
